Copper.TaskSchedule = (function() {
    var mockData = [{
        id: 1, startTime:'20120610 00:00', endTime:'20120610 03:00', content: '1Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 2, startTime:'20120610 03:00', endTime:'20120610 05:30', content: '2Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 3, startTime:'20120610 06:30', endTime:'20120610 07:00', content: '3Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {    
        id: 4, startTime:'20120610 08:00', endTime:'20120610 09:00', content: '4Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 5, startTime:'20120610 10:45', endTime:'20120610 11:20', content: '5Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 6, startTime:'20120610 11:30', endTime:'20120610 12:00', content: '6Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 7, startTime:'20120611 06:30', endTime:'20120611 08:00', content: '7Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 8, startTime:'20120611 10:45', endTime:'20120611 11:20', content: '8Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 9, startTime:'20120612 06:30', endTime:'20120612 08:00', content: '9Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 10, startTime:'20120612 03:00', endTime:'20120612 05:30', content: '10Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 11, startTime:'20120612 00:00', endTime:'20120612 03:00', content: '11Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 12, startTime:'20120613 11:30', endTime:'20120613 12:00', content: '12Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }, {
        id: 13, startTime:'20120613 13:30', endTime:'20120613 15:00', content: '13Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. '
    }];
    
    var pageCount = 0;
    
    var pageSize = 5;
    
    var pageHeight = 450;
    
    var pageWidth = 310;
    
    var itemHeight = 80;
    
    var currentPage = 0;
    
    var showed = false;
    
    var el, body, footer;
    
    var date = new Date();
    
    var updateIndicator = function(){
        footer.querySelector('.copper-todo-index').innerHTML = currentPage + 1 + ' / ' + pageCount;
    };
    
    var dummy = document.createElement('LI');
    dummy.className = 'copper-insert-dummy';
    
    var currentDummyPos;
    
    var scheduleList = [];
    
    return {
        init: function(ct){
            el = ct;
            
            
            window.addEventListener('resize', function(){
                var h = document.body.clientHeight - document.body.querySelector('header').offsetHeight - 250;
                for(var i = 0; i < scheduleList.length; ++i){
                    scheduleList[i].setHeight(h);    
                }
            });
            
            Copper.TaskSchedule.render();
        },
        
        render: function(){
            var date = new Date(); //Copper.Global.currentDate
            var oDate = date;
            date.setFullYear(2012, 5, 10);
            
            
            scheduleList = [];
            for(var i = 0; i < 7; ++i){
                var data = [];
                var d = date.format('Ymd');
                var daySchedule;
               
                for(var j = 0; j < mockData.length; ++j){
                    if(mockData[j].startTime.indexOf(d) == 0){
                        data.push(mockData[j]);
                    }
                }
                daySchedule = new Copper.DaySchedule({
                    data: data,
                    container: el.firstChild,
                    date: d
                });
                scheduleList.push(daySchedule);
                
                if(i == 0){//todo
                    Copper.TaskManager.setData(data);
                    Copper.TaskManager.refresh();    
                }

                date.setDate(date.getDate() + 1);
            }
        },
        
        load: function(){
        },
        
        show: function(){ 
            el.style.display = 'block';  
        },
        
        hide: function(){ 
            el.style.display = 'none';  
        },
        
        createPage: function(){
            var page = document.createElement('DIV');
            page.innerHTML = '<div class="copper-todo-header" style="color:white;font-weight:bold;font-size:24px;">' +
                '<div style="">' + date.format('l') + '</div>' +
                '<div style="">' + date.format('M d') + '</div>' +
              '</div>' +
              '<ul class="copper-todo-body"></ul>' +
              '<div class="copper-todo-footer">' +
                '<div class="copper-todo-previous">&lt;</div>' +  
                '<div class="copper-todo-next">&gt;</div>' +
                '<div class="copper-todo-index"></div>' +
                '<div class="clear-all"></div>' +
              '</div>';
            page.className = 'copper-schedule-page';
            return page;
        },
        
        createDummyItem: function(pos){
            var page = el.childNodes[pos[0]]; 
            if(currentDummyPos){
                if(currentDummyPos[0] == pos[0] && currentDummyPos[1] == pos[1])return;
                dummy.parentNode.removeChild(dummy);
            }
            console.log(pos)
            var item = page.querySelector('.copper-todo-body').childNodes[pos[1]];  
            
            page.querySelector('.copper-todo-body').insertBefore(dummy, item);
            currentDummyPos = [];
            currentDummyPos.push(pos[0]);
            currentDummyPos.push(pos[1]);
            //page.appendChild(dummy);
        },
        
        getEventTarget: function(e){
            var x = e.clientX + window.pageXOffset;
            var y = e.clientY + window.pageYOffset;
            
            var left = el.offsetLeft;
            var right = left + pageCount * pageWidth;
            var top = el.offsetTop + 95;
            var bottom = top + pageHeight;
            if(x < left || x > right || y < top || y > bottom)return null;
            
            var col = Math.ceil((x - left) / pageWidth) - 1;
            var row = Math.ceil((y - top) / itemHeight) - 1;
            return [col, row];
        },
        
        reset: function(){
             /*
             if(dummy.parentNode){
                 dummy.parentNode.removeChild(dummy);
             }
             currentDummyPos = null;
             */
             for(var i = 0; i < scheduleList.length; ++i){
                  
                 scheduleList[i].selectedItem = null;
                 if(scheduleList[i].deleteBtn){
                     scheduleList[i].deleteBtn.style.display = 'none';  
                 } 
             }
             var selected = el.querySelector('.copper-todo-selected');
             if(selected){
                 selected.className = 'copper-todo-item';
             }
        }
    };
})();